<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Announcement extends AdminController {

	function __construct() { 
		parent::__construct();
		$this -> load -> model("Announcement_model");
	}

	public function Index() {
		$this -> load -> view('admin/announcementList');
	}

	function getDatas($params = array(), $isExport = false) {
		$connar = array();
		if (!$isExport) {
			$params = $_POST;
			$connar = $this -> Announcement_model -> pickPages($connar, $params);
		}
		$connar['items']="db_announcement.*,
						ifnull((
							SELECT
								GROUP_CONCAT(`name`)
							FROM
								db_role
							WHERE
								find_in_set(id,db_announcement.role_id_str)
						),'--') role_char,a.username creator_char";
		$connar['join']=array('admin a','a.id=db_announcement.creator');
		$connar['orderby'] = " db_announcement.created desc ";
		$connar['where']=$this->sqlEscape(" db_announcement.status=?? ",array(CommonStatus::UnDeleted[XPHEnum::Value]));
		
		
		if (isset($params['param'])) {
			$connar['where'] .= $this->sqlLikeEscape(" and (db_announcement.title like '%??%' ",array($params['param']));
			$connar['where'] .= $this->sqlLikeEscape(" or db_announcement.content like '%??%') ",array($params['param']));
		}

		if (!$isExport) {
			$data = $this -> Announcement_model -> get_page_list($connar);
		} else {
			$data["ret"] = $this -> Announcement_model -> get_list_full($connar);
		}

		foreach ($data['ret'] as $k => $v) {	
			$data['ret'][$k]['created_char']=date('Y-m-d H:i:s',$v['created']);

			$announcement_time='';
			if($v['start']&&$v['start']!=''){
				$announcement_time.=date('Y-m-d',$v['start']);
			}
			else{
				$announcement_time.='开始时间未设置';
			}
			$announcement_time.='——';
			if($v['end']&&$v['end']!=''){
				$announcement_time.=date('Y-m-d',$v['end']);
			}
			else{
				$announcement_time.='结束时间未设置';
			}
			$data['ret'][$k]['announcement_time']=$announcement_time;

		}
		if (!$isExport) {
			echo json_encode($data);
		} else {
			return $data["ret"];
		}
	}

	//新增，修改
	function edit($id){
		//角色数组
		$this -> load -> model("Role_model");
		$connar['where']=" id<>1 ";
		$data['role']=$this->Role_model->get_list_full($connar);
		$role_id='';
		$num=0;
		foreach ((new WebRole)->getAll() as $k => $v) {
			$num++;
			if($num==1){
				$role_id.=$v[XPHEnum::Value];
			}else{
				$role_id.=','.$v[XPHEnum::Value];
			}
		}
		array_unshift($data['role'],array('name'=>'全体','id'=>$role_id));

		if($id == 0){
			$data['title'] = "新增公告";
			$data['detail'] = $this -> Announcement_model -> get_single(array("id" => $id));
		}else{
			$data['title'] = "编辑公告";
			$data['detail'] = $this -> Announcement_model -> get_single(array("id" => $id));
		}
		$this -> load -> view('admin/announcementEdit',$data);
	}
	//新增修改操作
	function save(){
		$_POST=$this->NOTXSS_POST;

		if (!isset($_POST['title'])) {
			exit(retJson("请填写标题.", false));
		}
		if (!isset($_POST['role_id_str'])) {
			exit(retJson("请选择可见角色.", false));
		}
		if ($_POST['start']=='') {
			exit(retJson("请选择开始时间.", false));
		}
		if ($_POST['end']=='') {
			exit(retJson("请选择结束时间.", false));
		}
		
		$announcement = copyArray($_POST,array("id","title",'start','end','role_id_str','content'));
		if($announcement['start']!=''){
			$announcement['start']=strtotime($announcement['start'].' 00:00:00');
		}
		if($announcement['end']!=''){
			$announcement['end']=strtotime($announcement['end'].' 23:59:59');
		}
		if($announcement['end']<$announcement['start']){
			exit(retJson("结束时间必须大于开始时间.", false));
		}
		$announcement['role_id_str']=implode(',',$announcement['role_id_str']);
		
		if(!isset($_POST['id']) || $_POST['id'] == ""){

			$announcement['status']=CommonStatus::UnDeleted[XPHEnum::Value];
			$announcement['creator']=getSess()['id'];
			$announcement['created']=time();
			$this -> Announcement_model -> add($announcement);
			$isAdd = 1;


			exit(retJson("添加成功.", true,$isAdd));
		}else{
				

			$isAdd = 0;
			$announcement['updated']=time();
			$this -> Announcement_model -> update($announcement);

			exit(retJson("修改成功.", true,$isAdd));
		}
	}

	function delete($id){
		$this->Announcement_model->update(array('id'=>$id,'status'=>CommonStatus::Deleted[XPHEnum::Value]));
		exit(retJson("删除成功.", true));
	}
	
	
}